package svgedit.gui;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.UIManager;

/**
 * A subclasses JFileChooser which changes the languages of the labels
 * 
 * @author SARJ3
 * 
 */
@SuppressWarnings("serial")
public class FileChooserDialog
	extends JFileChooser
{
    private static Frame frame;

    public static final int FILE_OPEN_DIALOG = 0;
    public static final int FILE_SAVEAS_DIALOG = 1;
    public static final int FILE_EXPORT_DIALOG = 2;
    
    
    /**
     * Creates a file chooser dialog of supplied type at the location of the 
     * supplied file
     * @param type FILE_OPEN_DIALOG | FILE_SAVEAS_DIALOG | FILE_EXPORT_DIALOG
     * @param file
     */
    public FileChooserDialog(int type, File file)
    {
	this(type,file.getName());
    }

    /**
     * Creates a file chooser dialog of supplied type at the location of the 
     * supplied file
     * @param type FILE_OPEN_DIALOG | FILE_SAVEAS_DIALOG | FILE_EXPORT_DIALOG
     * @param file
     */
    public FileChooserDialog(int type, String file)
    {
	super(file);
	frame = Frame.getInstance();
	setLanguage();
	this.updateUI();
	
	switch(type)
	{
	case FILE_OPEN_DIALOG:
	    setAsOpenDialog();
	    break;
	case FILE_SAVEAS_DIALOG:
	    setAsSaveDialog();
	    break;
	case FILE_EXPORT_DIALOG:
	    setAsExportDialog();
	    break;
	    default:
		System.err.println("Incorrect Dialog Type");
	}
	
	
    }

    /**
     * Sets up the file chooser dialog for opening files
     */
    public void setAsOpenDialog()
    {
	addChoosableFileFilter(SupportedFiles.FileFilters.SVG_FILES_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.SVG_FILE_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.SVGZ_FILE_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.STAR_DOT_STAR);
	setAcceptAllFileFilterUsed(false);
	setFileFilter(SupportedFiles.FileFilters.SVG_FILES_FILTER);
	setDialogTitle(frame.getLanguageValue("TitleOpen"));

	updateUI();
    }
    
    

    /**
     * Sets up the file chooser dialog for saving files
     */
    private void setAsSaveDialog()
    {
	addChoosableFileFilter(SupportedFiles.FileFilters.SVG_FILES_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.SVG_FILE_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.SVGZ_FILE_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.STAR_DOT_STAR);
	setAcceptAllFileFilterUsed(false);
	setFileFilter(SupportedFiles.FileFilters.SVG_FILES_FILTER);
	setDialogTitle(frame.getLanguageValue("TitleSaveAs"));

	updateUI();
    }

    /**
     * Sets up the file chooser dialog for exporting files
     */
    private void setAsExportDialog()
    {
	addChoosableFileFilter(SupportedFiles.FileFilters.PNG_FILE_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.PDF_FILE_FILTER);
	addChoosableFileFilter(SupportedFiles.FileFilters.STAR_DOT_STAR);
	// if this is false its the default, wbich sux
	setAcceptAllFileFilterUsed(false);
	setFileFilter(SupportedFiles.FileFilters.PNG_FILE_FILTER);

	setDialogTitle(frame.getLanguageValue("TitleExportAs"));
	updateUI();	
    }

    
    /**
     * Sets each label to the current local language
     * 
     */
    private static void setLanguage()
    {
	UIManager.put("FileChooser.lookInLabelText",
		frame.getLanguageValue("lookInLabelText"));


	UIManager.put("FileChooser.saveInLabelText",
		frame.getLanguageValue("saveInLabelText"));
	
	UIManager.put("FileChooser.exportInLabelText",
		frame.getLanguageValue("exportInLabelText"));



	UIManager.put("FileChooser.filesOfTypeLabelText",
		frame.getLanguageValue("filesOfTypeLabelText"));

	UIManager.put("FileChooser.upFolderToolTipText",
		frame.getLanguageValue("upFolderToolTipText"));

	UIManager.put("FileChooser.fileNameLabelText",
		frame.getLanguageValue("fileNameLabelText"));

	UIManager.put("FileChooser.newFolderToolTipText",
		frame.getLanguageValue("newFolderToolTipText"));

	UIManager.put("FileChooser.listViewButtonToolTipText",
		frame.getLanguageValue("listViewButtonToolTipText"));

	UIManager.put("FileChooser.detailsViewButtonToolTipText",
		frame.getLanguageValue("detailsViewButtonToolTipText"));

	UIManager.put("FileChooser.saveButtonText",
		frame.getLanguageValue("saveButtonText"));

	UIManager.put("FileChooser.openButtonText",
		frame.getLanguageValue("openButtonText"));

	UIManager.put("FileChooser.cancelButtonText",
		frame.getLanguageValue("cancelButtonText"));

	UIManager.put("FileChooser.updateButtonText",
		frame.getLanguageValue("updateButtonText"));

	UIManager.put("FileChooser.helpButtonText",
		frame.getLanguageValue("helpButtonText"));

	UIManager.put("FileChooser.saveButtonToolTipText",
		frame.getLanguageValue("saveButtonToolTipText"));

	UIManager.put("FileChooser.detailsViewButtonToolTipText",
		frame.getLanguageValue("detailsViewButtonToolTipText"));

	UIManager.put("FileChooser.openButtonToolTipText",
		frame.getLanguageValue("openButtonToolTipText"));

	UIManager.put("FileChooser.cancelButtonToolTipText",
		frame.getLanguageValue("cancelButtonToolTipText"));

	UIManager.put("FileChooser.updateButtonToolTipText",
		frame.getLanguageValue("updateButtonToolTipText"));

	UIManager.put("FileChooser.helpButtonToolTipText",
		frame.getLanguageValue("helpButtonToolTipText"));
    }

    
}
